import json
import pandas as pd
import csv

def rowdaata2csv():
    # 1.从josn文件中读取数据，并保存在josn_data变量中
    json_data = open('../data/model/testdata.txt', encoding='utf-8').read()
    # print(type(json_data)) #str
    # 2.json调用loads()方法将字符串数据转换成列表
    data = json.loads(json_data)
    #print(type(data)) #list
    dictArr = []
    for item in data:
        #print(type(item))  -->dict
        dict = {}
        if item is None:
            continue

        if 'description' in item:
            dict['des'] = item['description']

        if 'tags' in item:
            tags = item['tags']
            tagStr = ''
            count = 0
            for tag in tags:
                tagStr += tag + ' '
                count += 1
                if count == 3:
                    break
            dict['tags'] = tagStr[:-1]
        dictArr.append(dict)

    pd.DataFrame(dictArr).to_csv('../data/model/testdata.csv',index=False)

def csv2labbled_doc():
    datalist = []

    with open('../data/model/testdata.csv', 'r', encoding='utf-8')as f:
        tags_des_list = list(csv.reader(f))
    del(tags_des_list[0])

    tuplelist = []
    for item in tags_des_list:
        # item 是list[str,str]，需要转换成list[str,list]
        taglist = item[1].split()
        sublist = []
        sublist.append(item[0]*4)
        sublist.append(taglist)
        t = tuple(sublist)
        tuplelist.append(t)
    return tuplelist



rowdaata2csv()
# csv2labbled_doc()
# testType()